﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AmiEmuLib.AmigaConfiguration;
using AmiEmuLib.AmigaMachine.AmigaCPU;
using AmiEmuLib.AmigaMachine.AmigaMemory;
using AmiEmuLib.SaveState;

namespace AmiEmuLib.AmigaMachine
{
    /// <summary>
    /// Represent amiga machine.
    /// </summary>
    public class Machine
    {
        /// <summary>
        /// Amiga configuration. Do not change.
        /// </summary>
        public readonly AmigaConfig Config;

        /// <summary>
        /// Amiga cpu.
        /// </summary>
        public readonly CPU680x0 CPU;

        /// <summary>
        /// Amiga memory.
        /// </summary>
        public readonly Memory Memory;

        /// <summary>
        /// Reset event.
        /// </summary>
        internal event Action OnReset;

        /// <summary>
        /// Create machine from config.
        /// </summary>
        /// <param name="a_config"></param>
        public Machine(AmigaConfig a_config)
        {
            Config = new AmigaConfig(a_config);
            Memory = new Memory(this);
            CPU = CPU680x0.Create(this);

            Reset();
        }

        /// <summary>
        /// Create machine from save state.
        /// </summary>
        /// <param name="a_state"></param>
        public Machine(State a_state) 
            : this(a_state.GetConfig())
        {
            a_state.Restore(this);
        }

        /// <summary>
        /// Reset amiga machine.
        /// </summary>
        public void Reset()
        {
            if (OnReset != null)
                OnReset();
        }
    }
}
